{ "cells": [ { "cell_type": "markdown", "id": "4f641d0b", "metadata": {}, "source": [ "# Tutorial 1: Encoding histology image features using H-ViT" ] }, { "cell_type": "markdown", "id": "08ea17b8", "metadata": {}, "source": [ "This tutorial shows how to apply H-ViT to extract the latent representation of each spot image. Here we take the slice 151674 of the DLPFC dataset as example. The histology image is available at https://github.com/LieberInstitute/spatialLIBD.\n", "To reduce your waiting time, we upload obtained histological image representation data to https://zenodo.org/record/8185216/files/DLPFC.rar?download=1." ] }, { "cell_type": "markdown", "id": "62f8d5ad", "metadata": {}, "source": [ "## Preparation" ] }, { "cell_type": "code", "execution_count": 1, "id": "2c712a75", "metadata": { "scrolled": true }, "outputs": [], "source": [ "import warnings\n", "warnings.filterwarnings(\"ignore\")\n", "import pandas as pd\n", "import os\n", "from stGCL.image_processing import image_representation\n", "import os\n", "import torch\n", "import random\n", "import numpy as np\n", "from torch.backends import cudnn\n", "import pandas as pd\n", "from stGCL.process import set_seed" ] }, { "cell_type": "code", "execution_count": 2, "id": "4fe82954", "metadata": {}, "outputs": [], "source": [ "# seed=0\n", "# set_seed(seed)\n", "section_id=\"151676\"\n", "pca_num = 3000" ] }, { "cell_type": "code", "execution_count": 3, "id": "b21db3cb", "metadata": {}, "outputs": [], "source": [ "labeldf = pd.read_csv(\"/home/dell/stproject/stGCL/Data/DLPFC/{}/cluster_labels_{}.csv\".format(section_id, section_id), sep=\",\", header=0,\n", " na_filter=False, index_col=0)\n", "label = labeldf[\"ground_truth\"]" ] }, { "cell_type": "code", "execution_count": 4, "id": "503c3a56", "metadata": {}, "outputs": [], "source": [ "positionsdata = pd.read_csv(\"/home/dell/stproject/stGCL/Data/DLPFC/{}/spatial/tissue_positions_list.csv\".format(section_id), sep=\",\", header=None,\n", " na_filter=False, index_col=0)\n", "positionsdata = positionsdata.loc[positionsdata[1] == 1].iloc[: , 3:5]\n", "positionsdata.columns = ['x','y']" ] }, { "cell_type": "code", "execution_count": 5, "id": "b33c6069", "metadata": {}, "outputs": [], "source": [ "scale_file=\"/home/dell/stproject/stGCL/Data/DLPFC/{}/spatial/scalefactors_json.json\".format(section_id)\n", "image_adress=\"/home/dell/stproject/stGCL/Data/DLPFC/{}/spatial\".format(section_id)\n", "score_adress=\"/home/dell/stproject/stGCL/image_representation/\".format(section_id)\n", "image_file=\"tissue_hires_image.png\"" ] }, { "cell_type": "markdown", "id": "167632cc", "metadata": {}, "source": [ "## Extract histology image features" ] }, { "cell_type": "code", "execution_count": 6, "id": "99d03109", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Loading image data...\n" ] }, { "name": "stderr", "output_type": "stream", "text": [ "Tiling image: 100%|████████████████████████████████████████████████████████████████████████████████ [ time left: 00:00 ]\n", "extract representation by ViT: 100%|████████████████████████████████████████████████████| 28/28 [00:09<00:00, 2.98it/s]\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "PCA dimensionality reduction\n", "Image features are stored in: /home/dell/stproject/stGCL/image_representation//ViT_representation.csv\n", "PCA Image features are stored in: /home/dell/stproject/stGCL/image_representation//ViT_pca_representation.csv\n", "Complete the extraction of image representation\n" ] } ], "source": [ "image_representation(positionsdata,image_adress,image_file,label,score_adress,pca_num=3000,k=7,scale_file=scale_file,\n", " image_net=\"ViT\",image_type=\"hires\",crop_size=256,patch_size=64,load=False,batch_size_lw=128)" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.7.12" } }, "nbformat": 4, "nbformat_minor": 5 }